using System;
using System.Collections;
using System.Collections.Generic;
using Server;
using Server.Targeting;
using Server.Items;
using Server.Network;
using Server.ContextMenus;

namespace Server.Mobiles
{
	public class RepairingArmorer : BaseVendor
	{
		private List<SBInfo> m_SBInfos = new List<SBInfo>();
		protected override List<SBInfo> SBInfos{ get { return m_SBInfos; } }

		[Constructable]
		public RepairingArmorer() : base( "the armor repairer" )
		{
			SetSkill( SkillName.ArmsLore, 64.0, 100.0 );
			SetSkill( SkillName.Blacksmith, 60.0, 83.0 );
		}

		public override void InitSBInfo()
		{
			switch ( Utility.Random( 4 ))
			{
				case 0:
				{
					m_SBInfos.Add( new SBLeatherArmor() );
					m_SBInfos.Add( new SBStuddedArmor() );
			        m_SBInfos.Add( new SBMetalShields() );
					m_SBInfos.Add( new SBPlateArmor() );
			        m_SBInfos.Add( new SBHelmetArmor() );
			        m_SBInfos.Add( new SBChainmailArmor() );
			        m_SBInfos.Add( new SBRingmailArmor() );
					break;
				}
				case 1:
				{
			        m_SBInfos.Add( new SBStuddedArmor() );
			        m_SBInfos.Add( new SBLeatherArmor() );
					m_SBInfos.Add( new SBMetalShields() );
					m_SBInfos.Add( new SBHelmetArmor() );
					break;
				}
				case 2:
				{
					m_SBInfos.Add( new SBMetalShields() );
					m_SBInfos.Add( new SBPlateArmor() );
					m_SBInfos.Add( new SBHelmetArmor() );
					m_SBInfos.Add( new SBChainmailArmor() );
					m_SBInfos.Add( new SBRingmailArmor() );
					break;
				}
				case 3:
				{
					m_SBInfos.Add( new SBMetalShields() );
					m_SBInfos.Add( new SBHelmetArmor() );
					break;
				}
			}
		}

		public override VendorShoeType ShoeType
		{
			get{ return VendorShoeType.Boots; }
		}

		public override void InitOutfit()
		{
			base.InitOutfit();

			AddItem( new Server.Items.HalfApron( Utility.RandomYellowHue() ) );
        }

		private class FixEntry : ContextMenuEntry
		{
			private RepairingArmorer m_Armorer;
			private Mobile m_From;

			public FixEntry( RepairingArmorer Armorer, Mobile from ) : base( 6120, 12 )
			{
				m_Armorer = Armorer;
				m_From = from;
			}

			public override void OnClick()
			{
				m_Armorer.BeginRepair( m_From );
			}
		}

		public override void AddCustomContextEntries( Mobile from, List<ContextMenuEntry> list )
		{
			if ( from.Alive )
			{
				list.Add( new FixEntry( this, from ) );
			}

			base.AddCustomContextEntries( from, list );
		}

		public override bool HandlesOnSpeech( Mobile from )
		{
			if( from.InRange( this.Location, 8 ) )
				return true;

			return base.HandlesOnSpeech( from );
		}

        public override void OnSpeech(SpeechEventArgs e)
        {
            if ((e.Speech.ToLower() == "repair"))
            {
                BeginRepair(e.Mobile);
            }

            else
            {
                base.OnSpeech(e);
            }
        }
        public void BeginRepair(Mobile from)
        {
            if (Deleted || !from.CheckAlive())
                return;

            SayTo(from, "You want to hire me to repair what?");

            from.Target = new RepairTarget(this);
        }

        private class RepairTarget : Target
        {
            private RepairingArmorer m_Armorer;

            public RepairTarget(RepairingArmorer armorer) : base(12, false, TargetFlags.None)
            {
                m_Armorer = armorer;
            }

            protected override void OnTarget(Mobile from, object targeted)
            {
                if (targeted is BaseArmor && from.Backpack != null)
                {
                    BaseArmor ba = targeted as BaseArmor;
                    Container pack = from.Backpack;
                    int toConsume = 0;

                    if (ba.HitPoints < ba.MaxHitPoints && ba.Resource != CraftResource.RegularLeather && ba.Resource != CraftResource.SpinedLeather && ba.Resource != CraftResource.HornedLeather && ba.Resource != CraftResource.BarbedLeather )
                    {
                        toConsume = (ba.MaxHitPoints - ba.HitPoints) * 2; // 2 gp per hitpoint - change 2 to whatever you want as a multiplier
                    }
                    else if (ba.HitPoints >= ba.MaxHitPoints && ba.Resource != CraftResource.RegularLeather && ba.Resource != CraftResource.SpinedLeather && ba.Resource != CraftResource.HornedLeather && ba.Resource != CraftResource.BarbedLeather )
                    {
						m_Armorer.SayTo(from, "That does not need to be repaired.");
                    }
					else
					{
						m_Armorer.SayTo(from, "I cannot repair that.");
					}

                    if (toConsume == 0)
                        return;

                    if (pack.ConsumeTotal(typeof(Gold), toConsume))
                    {
                        m_Armorer.SayTo(from, "Here is your armor.");
                        from.SendMessage(String.Format("You pay {0} gold.", toConsume));
                        Effects.PlaySound(from.Location, from.Map, 0x2A);
                        ba.MaxHitPoints -= 1;
                        ba.HitPoints = ba.MaxHitPoints;
                    }
                    else
                    {
                        m_Armorer.SayTo(from, "It would cost you {0} gold to have that repaired.", toConsume);
                        from.SendMessage("You do not have enough gold.");
                    }
                }
                else
                    m_Armorer.SayTo(from, "I cannot repair that.");
            }
        }

        public RepairingArmorer( Serial serial ) : base( serial )
		{
		}

		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );
			writer.Write( (int) 0 ); // version
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );
			int version = reader.ReadInt();
		}
	}
}